package com.goodving.guli.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.goodving.guli.R;
import com.goodving.guli.bt.C0668a;
import com.goodving.guli.bt.DimPanelFragment;
import com.goodving.guli.bt.Utils;
import com.goodving.guli.bt.eventbus.EventPersonInfoUpdate;
import com.goodving.guli.bt.eventbus.EventUserInfoUpdate;
import com.goodving.guli.bt.model.PersonInfo;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.ui.PickAdapter;
import com.goodving.guli.bt.weight.UserInfo;
import com.goodving.guli.bt.weight.UserInfoManager;
import com.goodving.guli.model.Keeper;
import com.goodving.guli.view.C3657a;
import com.goodving.guli.view.C4220a;

import de.greenrobot.event.EventBus;
import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;

/**
 * Created by john on 2015/12/24.
 */
public class HeightPanelFragment extends DimPanelFragment {
    private static final String f9938a = "Height";
    private static int f9939o;
    private PersonInfo f9940b;
    private UserInfo f9941c;
    private WheelView f9942d;
    private WheelView f9943e;
    private PickAdapter f9944f;
    private PickAdapter f9945g;
    private int f9946h;
    private int f9947i;
    private int f9948j;
    private int f9949k;
    private View f9950l;
    private int f9951m;
    private int f9952n;

    public HeightPanelFragment() {
        this.f9948j = 0;
        this.f9949k = 0;
    }

    public static Fragment m14650a(int i) {
        GuliLog.tracePrintInfo(f9938a, "receive the uid " + i);
        f9939o = i;
        return new HeightPanelFragment();
    }

    class df implements OnWheelScrollListener {
        final /* synthetic */ HeightPanelFragment f9953a;

        df(HeightPanelFragment deVar) {
            this.f9953a = deVar;
        }

        public void onScrollingStarted(WheelView wheelView) {
        }

        public void onScrollingFinished(WheelView wheelView) {
            this.f9953a.m14654a(this.f9953a.f9942d, this.f9953a.f9949k);
        }
    }

    class dg implements OnWheelScrollListener {
        final /* synthetic */ HeightPanelFragment f9954a;

        dg(HeightPanelFragment deVar) {
            this.f9954a = deVar;
        }

        public void onScrollingStarted(WheelView wheelView) {
        }

        public void onScrollingFinished(WheelView wheelView) {
            this.f9954a.m14656b();
            this.f9954a.m14654a(wheelView, 1);
        }
    }


    private void m14652a() {
        this.f9943e.setVisibility(View.VISIBLE);
        this.f9950l.setVisibility(View.VISIBLE);
        this.f9944f = new PickAdapter(getActivity(), 1, 7, this.f9943e, getResources().getColor(R.color.highlight), getResources().getColor(R.color.main_ui_content_color), C4220a.bm, false, 46, 24, 21, 21, 1);
        this.f9943e.setCenterDrawable(R.drawable.wheel_custom_val_white_1).setCenterStyle(getString(R.string.unit_foot), (float) C4220a.bf).setViewAdapter(this.f9944f).setCurrentItem(m14659d());
        m14656b();
        m14654a(this.f9943e, 1);
        m14654a(this.f9942d, this.f9949k);
        this.f9942d.addScrollingListener(new df(this));
        this.f9943e.addScrollingListener(new dg(this));
    }

    private void m14654a(WheelView wheelView, int i) {
        if (isAdded()) {
            String quantityString;
            int f = wheelView.getRealCurrentItem() + i;
            if (wheelView == this.f9942d) {
                quantityString = getResources().getQuantityString(R.plurals.numberInch, f, new Object[]{C3657a.f18328f});
            } else {
                quantityString = getResources().getQuantityString(R.plurals.numberFoot, f, new Object[]{C3657a.f18328f});
            }
            GuliLog.tracePrintInfo(f9938a, "value = " + f + " unit -= " + quantityString);
            wheelView.setCenterStyle(quantityString);// m26784a
            wheelView.invalidateWheel(false);
        }
    }

    private void m14656b() {
        int f = this.f9942d.getRealCurrentItem() + this.f9949k;
        if (this.f9943e.getRealCurrentItem() + 1 == 7) {
            this.f9949k = 0;
            this.f9948j = 6;
        } else if (this.f9943e.getRealCurrentItem() + 1 == 1) {
            this.f9949k = 4;
            this.f9948j = 11;
        } else {
            this.f9949k = 0;
            this.f9948j = 11;
        }
        if (!(this.f9948j == this.f9946h && this.f9949k == this.f9947i)) {
            this.f9945g.m14693c(this.f9949k);
            this.f9945g.m14695d(this.f9948j);
            GuliLog.tracePrintInfo(f9938a, "onScrollingFinished : " + this.f9943e.getRealCurrentItem() + ", invalidate, mInchMin =  " + this.f9949k + ",mInchMax = " + this.f9948j);
            if (f > this.f9948j) {
                f = this.f9948j;
            } else if (f < this.f9949k) {
                f = this.f9949k;
            }
            this.f9942d.setCurrentItem(f - this.f9949k, true);
            this.f9942d.invalidateWheel(true);
        }
        this.f9946h = this.f9948j;
        this.f9947i = this.f9949k;
    }

    private int m14657c() {
        if (this.f9951m != 1) {
            return this.f9952n - 40;
        }
        int a = Utils.m7962a(this.f9952n) % 12;
        if (Utils.m7962a(this.f9952n) / 12 <= 1) {
            this.f9949k = 4;
        } else {
            this.f9949k = 0;
        }
        GuliLog.tracePrintInfo(f9938a, "inches = " + a + ", min = " + this.f9949k);
        return a - this.f9949k;
    }

    private int m14659d() {
        return (Utils.m7962a(this.f9952n) / 12) - 1;
    }

    protected int inflateLayout() {
        return R.layout.fragment_person_info_setting_height;
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        if (f9939o == -1) {
            this.f9940b = Keeper.readPersonInfo();
            this.f9952n = this.f9940b.height;
        } else {
            this.f9941c = UserInfoManager.getInstance().getInfoByUid(f9939o);
            this.f9952n = this.f9941c.height;
        }
        this.f9951m = Keeper.readPersonInfo().getUnit();
    }

    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View onCreateView = super.onCreateView(layoutInflater, viewGroup, bundle);
        this.f9942d = (WheelView) onCreateView.findViewById(R.id.info_height_wheel);
        this.f9945g = new PickAdapter(getActivity(), this.f9951m == 0 ? 40 : 0, this.f9951m == 0 ? C4220a.an : 11, this.f9942d, getResources().getColor(R.color.highlight), getResources().getColor(R.color.main_ui_content_color), C4220a.bm, false, 46, 24, 21, 21, 1);
        this.f9942d.setCenterDrawable(R.drawable.wheel_custom_val_white_1).setCenterStyle(this.f9951m == 0 ? getString(R.string.cm) : getString(R.string.unit_inch), (float) C4220a.bf).setViewAdapter(this.f9945g).setCurrentItem(m14657c());
        this.f9943e = (WheelView) onCreateView.findViewById(R.id.info_height_foot_wheel);
        this.f9950l = onCreateView.findViewById(R.id.wheel_split_line);
        if (this.f9951m == 0) {
            this.f9943e.setVisibility(View.GONE);
            this.f9950l.setVisibility(View.GONE);
        } else {
            m14652a();
        }
        return onCreateView;
    }

    public void onDestroy() {
        super.onDestroy();
        C0668a.m8127a(getActivity(), C0668a.dX, this.f9952n);
    }

    public void onPause() {
        super.onPause();
        C0668a.m8141b(C0668a.f3567j);
    }

    public void onResume() {
        super.onResume();
        C0668a.m8133a(C0668a.f3567j);
    }

    protected void onRightButtonClicked() {
        int i = this.f9952n;
        if (this.f9951m == 1) {
            int f = this.f9943e.getRealCurrentItem() + 1;
            int f2 = this.f9942d.getRealCurrentItem() + this.f9949k;
            int i2 = (f * 12) + f2;
            GuliLog.tracePrintInfo(f9938a, "set height : feet = " + f + "inch = " + f2);
            f = (int) Math.round(((double) i2) * 2.54d);
            if (this.f9940b != null) {
                this.f9940b.height = f;
            } else {
                this.f9941c.height = f;
            }
        } else if (this.f9940b != null) {
            this.f9940b.height = this.f9942d.getRealCurrentItem() + 40;
        } else {
            this.f9941c.height = this.f9942d.getRealCurrentItem() + 40;
        }
        if (this.f9940b != null) {
            if (i == this.f9940b.height) {
                dismiss();
                return;
            }
            this.f9940b.setNeedSyncServer(2);
            Keeper.keepPersonInfo(this.f9940b);
            EventBus.getDefault().post(new EventPersonInfoUpdate(null)); //TBD
            dismiss();
        } else if (i == this.f9941c.height) {
            dismiss();
        } else {
            EventBus.getDefault().post(new EventUserInfoUpdate(this.f9941c));
            dismiss();
        }
    }
}
